//
//  NNEnvSelectViewController.m
//  MockQR
//
//  Created by fangshufeng on 2018/3/5.
//  Copyright © 2018年 fangshufeng. All rights reserved.
//

#import <AFNetworking/AFNetworking.h>

#import "NNEnvSelectViewController.h"
#import "NNEnvContainerView.h"
#import "NNMockQrViewController.h"
//#import "MZGETMockAPi.h"

@interface NNEnvSelectViewController () <UIScrollViewDelegate,NNMockQrViewControllerDelegate>

//@property (nonatomic, strong) NSDictionary *dataDic;

/**
 * 环境数组
 */
@property (nonatomic, copy) NSArray  *envList;

@property (nonatomic, weak) UIScrollView *bgScrollView;

/**
 * 复制
 */
@property (nonatomic, strong) NSMutableArray *copyBlocks ;

/**
 * 关联的block
 */
@property (nonatomic, strong) NSMutableArray *relationBlocks;

/**
 * 当前环境
 */
@property (nonatomic, assign) NSInteger currentIndex;

/**
 * input
 */
@property (nonatomic, assign) CGRect inputRelativeFrame;

@property (nonatomic, weak) UITextField *inputF;

/**
 * mock相关
 */
//@property (nonatomic, strong) MZGETMockAPi *mockApi;

/**
 * 开发者调试用
 */
@property (nonatomic, weak) UITextField *developTextField;

@property (nonatomic, weak) UIButton *btnLock;

/**
 * laoding
 */
@property (nonatomic, weak) UIView *loadingView;

@end

@implementation NNEnvSelectViewController

- (void)dealloc {
#if DEBUG
    NSLog(@"NNEnvSelectViewController---dealloc");
#endif
}

- (void)addObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleKeyShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleHiden:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)removeObserver {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title  = @"切换测试环境";
    UIScrollView *bgScrollView = [[UIScrollView alloc] init];
    bgScrollView.delegate =self;
    bgScrollView.frame = CGRectMake(0, 64, KNNENvWidth, KNNENvHeight - 209 - 64);
    [self.view addSubview:bgScrollView];
    self.bgScrollView = bgScrollView;
    
    self.envList = @[@"BETA",@"PROD",@"MOCK"];
    NNEnvContainerView *env = [[NNEnvContainerView alloc] initWithCol:3
                                                              envList:self.envList];
    env.frame = CGRectMake(0, CGRectGetMaxY(bgScrollView.frame) + 10, KNNENvWidth, env.calculateHeight);
    __weak typeof(self) wealSelf = self;
    env.itemClick = ^(NSInteger index) {
        __strong typeof(wealSelf) strongSelf = wealSelf;
        strongSelf.currentIndex = index;
        [strongSelf layoutSubviewsForScrollViewEnv:strongSelf.envList[index]];
    };
    [env selectIndex:self.defaultSelectIndex];
    [self.view addSubview:env];
    
    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(env.frame) + 20, KNNENvWidth - 16 * 2, 44)];
    [sureBtn addTarget:self action:@selector(sureBtnDid) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:KNNENv_rgba(41,107,255,1) forState:UIControlStateNormal];
    sureBtn.layer.borderColor = KNNENv_rgba(41,107,255,1).CGColor;
    sureBtn.layer.borderWidth = 1;
    sureBtn.layer.cornerRadius = 8;
    sureBtn.clipsToBounds = YES;
    [self.view addSubview:sureBtn];
    
    self.customNavItem.rightBarButtonItem = [self rightBarButton];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self addObserver];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self removeObserver];
}
#pragma mark - public
- (void)sureBtnDid {
    if (self.currentIndex == [self.envList indexOfObject:@"MOCK"]) {
        if (self.inputF.text.length == 0) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入关联码" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }]];
            [self presentViewController:alert animated:YES completion:nil];
        
            return;
        }
        [self openMockAction];
    } else {
        if (self.envSelectAction) {
            self.envSelectAction(self.currentIndex, nil);
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)btnDid:(UIButton *)btn {
    void (^copyBlock)(void) = self.copyBlocks[btn.tag];
    copyBlock();
}

#pragma mark - 私有操作
- (void)relationBtnClick:(UIButton *)btn {
    void (^relationBlock)(void) = self.relationBlocks[btn.tag];
    relationBlock();
}

- (void)openMockAction {
    
//    self.mockApi.socketKey = self.inputF.text;
    __weak typeof(self) weakSelf = self;
    self.loadingView =  [self startloadView];
    NSDictionary *dict = @{@"socketKey":self.inputF.text};
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [manager GET:[NSString stringWithFormat:@"%@/mock/uploadDeviceIdentifier", self.developTextField.text] parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        __weak typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.loadingView removeFromSuperview];
        if (responseObject) {
            NSDictionary *dict = responseObject;
            if ([dict[@"errorCode"] isEqualToString:@"0000000"]) {
                if (self.envSelectAction) {
                    self.envSelectAction(self.currentIndex, [dict[@"data"] objectForKey:@"proxyUrl"]);
                }
    
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"开启mock成功！" preferredStyle:UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }]];
                [self presentViewController:alert animated:YES completion:nil];
            }else {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:dict[@"errorMessage"] preferredStyle:UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }]];
                [self presentViewController:alert animated:YES completion:nil];
                return ;
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        __weak typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.loadingView removeFromSuperview];
        
    }];

    return;
    
}

- (void)lock {
    BOOL selected = self.developTextField.enabled;
    self.developTextField.enabled = !selected;
    self.btnLock.selected = !selected;
    if (selected) {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setValue:self.developTextField.text forKey:@"MockQR_ServerAddress"];
        [userDefaults synchronize];
    }
}

- (void)qrScanVc:(NNMockQrViewController *)vc didCatchQRCodeString:(NSString *)result {
    [vc dismissViewControllerAnimated:YES completion:^{
        NSString *mockPre = @"nonobankMock://";
        if ([result hasPrefix:mockPre]) { //mock数据专用
            self.inputF.text = [result substringFromIndex:mockPre.length];
        }
    }];
}


#pragma mark - subviews
- (void)layoutSubviewsForScrollViewEnv:(NSString *)env {
    
    [self.bgScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.copyBlocks removeAllObjects];
    [self.relationBlocks removeAllObjects];
    
    CGFloat lMargin = 16;
    
    UILabel *title = [self envLabelWithFrame:CGRectMake(lMargin, 20, 150, 36)
                                        font:[UIFont boldSystemFontOfSize:36]
                                       color:[UIColor blackColor]
                                        text:env
                               textAlignment:NSTextAlignmentLeft];
    
    
    [self.bgScrollView addSubview:title];
    
    
    UILabel *envTitle = [self envLabelWithFrame:CGRectMake(lMargin,CGRectGetMaxY(title.frame) + 40, 100, 21)
                                           font:[UIFont boldSystemFontOfSize:16]
                                          color:KNNENv_rgba(97,97,97,1)
                                           text:@"环境说明"
                                  textAlignment:NSTextAlignmentLeft];
    [self.bgScrollView addSubview:envTitle];
    
    
    UILabel *envDesc = [self envLabelWithFrame:CGRectMake(lMargin,CGRectGetMaxY(envTitle.frame) + 10, KNNENvWidth - (lMargin * 2) , 0)
                                          font:[UIFont boldSystemFontOfSize:13]
                                         color:[UIColor lightGrayColor]
                                          text:self.dataDic[env][@"envDesc"]
                                 textAlignment:NSTextAlignmentLeft];
    [self.bgScrollView addSubview:envDesc];
    
//    UILabel *netTitle = [self envLabelWithFrame:CGRectMake(lMargin,CGRectGetMaxY(envDesc.frame) + 20, 100, 21)
//                                           font:[UIFont boldSystemFontOfSize:16]
//                                          color:KNNENv_rgba(97,97,97,1)
//                                           text:@"网络说明"
//                                  textAlignment:NSTextAlignmentLeft];
//    [self.bgScrollView addSubview:netTitle];
    
    UILabel *netDesc = [self envLabelWithFrame:CGRectMake(lMargin,CGRectGetMaxY(envDesc.frame) + 10, KNNENvWidth - (lMargin * 2) , 21)
                                          font:[UIFont boldSystemFontOfSize:13]
                                         color:[UIColor lightGrayColor]
                                          text:self.dataDic[env][@"netDesc"]
                                 textAlignment:NSTextAlignmentLeft];
    
    [self.bgScrollView addSubview:netDesc];
    
    UILabel *dnsTitle = [self envLabelWithFrame:CGRectMake(lMargin,CGRectGetMaxY(netDesc.frame) + 20, 100, 21)
                                           font:[UIFont boldSystemFontOfSize:16]
                                          color:KNNENv_rgba(97,97,97,1)
                                           text:@"地址列表"
                                  textAlignment:NSTextAlignmentLeft];
    
    [self.bgScrollView addSubview:dnsTitle];
    
    NSArray *DNSList = self.dataDic[env][@"ENVList"];
    CGFloat dnsBeginY = CGRectGetMaxY(dnsTitle.frame);
    
    if ([DNSList count] == 0 ) {
        
        UILabel *dnsDesc = [self envLabelWithFrame:CGRectMake(lMargin,dnsBeginY + 10, KNNENvWidth - (lMargin * 2) , 21)
                                              font:[UIFont boldSystemFontOfSize:13]
                                             color:[UIColor lightGrayColor]
                                              text:@"无需设置"
                                     textAlignment:NSTextAlignmentLeft];
        
        [self.bgScrollView addSubview:dnsDesc];
    } else {
        UIView *line = [self lineWithFrame:CGRectMake(0,dnsBeginY + 19, KNNENvWidth , 1)];
        [self.bgScrollView addSubview:line];
        
        dnsBeginY = CGRectGetMaxY(line.frame);
        
        for (int i = 0; i < DNSList.count; i++) {
            NSArray *tempItem = DNSList[i];
            UIView *item =  [self dnsItemWithEnv:tempItem[0] dnsHost:tempItem[1]];
            item.frame = CGRectMake(0, dnsBeginY,KNNENvWidth , 44);
            [self.bgScrollView addSubview:item];
            dnsBeginY += 44;
        }
    }
    
    if ([env isEqualToString:@"MOCK"]) {
        UIView *line = [self lineWithFrame:CGRectMake(0,dnsBeginY + 0, KNNENvWidth, 0)];
        [self.bgScrollView addSubview:line];
        
        UILabel *mockHost = [self envLabelWithFrame:CGRectMake(16,  CGRectGetMaxY(line.frame) +  0, 100, 0)
                                               font:[UIFont systemFontOfSize:16]
                                              color:[UIColor grayColor]
                                               text:@""
                                      textAlignment:NSTextAlignmentLeft];
        [self.bgScrollView addSubview:mockHost];
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSString *serverAddress = [userDefaults stringForKey:@"MockQR_ServerAddress"];
        
        UITextField *mockDeafultHost = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(mockHost.frame),
                                                                                     CGRectGetMaxY(line.frame) +  7, KNNENvWidth - 100 - 55, 0)];
        mockDeafultHost.text = serverAddress ?: @"http://192.168.12.25:7778";
        mockDeafultHost.enabled = NO;
        mockDeafultHost.font = [UIFont systemFontOfSize:12];
        mockDeafultHost.backgroundColor = [UIColor cyanColor];
        [self.bgScrollView addSubview:mockDeafultHost];
        self.developTextField = mockDeafultHost;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(CGRectGetMaxX(mockDeafultHost.frame), CGRectGetMaxY(line.frame) + 3, 32, 39);
        NSBundle *bundle = [NSBundle bundleWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"MockQR.bundle"]];
        if (@available(iOS 8.0, *)) {
            [btn setImage:[UIImage imageNamed:@"MockQR_Lock.png" inBundle:bundle compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
        }
        if (@available(iOS 8.0, *)) {
            [btn setImage:[UIImage imageNamed:@"MockQR_Unlock.png" inBundle:bundle compatibleWithTraitCollection:nil] forState:UIControlStateSelected];
        }
        [btn addTarget:self action:@selector(lock) forControlEvents:UIControlEventTouchUpInside];
        [self.bgScrollView addSubview:btn];
        self.btnLock = btn;
        
        UIView *line2 = [self lineWithFrame:CGRectMake(0,CGRectGetMaxY(mockHost.frame), KNNENvWidth, 1)];
        [self.bgScrollView addSubview:line2];
        
        UIView *associatedMethod = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(line2.frame), KNNENvWidth, 125)];
        [self.bgScrollView addSubview:associatedMethod];
        
        UILabel *relationCodeTitle = [self envLabelWithFrame:CGRectMake(16, 16, KNNENvWidth - 16, 30)
                                                        font:[UIFont boldSystemFontOfSize:16]
                                                       color:[UIColor grayColor]
                                                        text:@"关联码获取"
                                               textAlignment:NSTextAlignmentLeft];
        [associatedMethod addSubview:relationCodeTitle];
        
        __weak typeof(self) weakSelf = self;
        UIView *method1 = [self methodViewWithFrame:CGRectMake(0, CGRectGetMaxY(relationCodeTitle.frame) + 5, KNNENvWidth, 47)
                                              title:@"方法一（推荐）："
                                         rightTitle:@"扫码获取"
                                             action:^{
                                                 __strong typeof(weakSelf) strongSelf = weakSelf;
                                                 NNMockQrViewController *qr = [[NNMockQrViewController alloc] init];
                                                 qr.delegate = strongSelf;
                                                 [strongSelf presentViewController:qr animated:YES completion:nil];
                                             }];
        [associatedMethod addSubview:method1];
        
        self.inputRelativeFrame = [method1 convertRect:self.inputF.frame toView:self.view];
        
        self.bgScrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(associatedMethod.frame));
    }else {
        self.bgScrollView.contentSize = CGSizeMake(0, dnsBeginY);
    }
}

- (UIView *)lineWithFrame:(CGRect )frame {
    UIView *lineView = [[UIView alloc] initWithFrame:frame];
    lineView.backgroundColor = KNNENv_rgba(224,224,224,0.5);
    return lineView;
}

- (UIView *)dnsItemWithEnv:(NSString *)env dnsHost:(NSString *)dnsHost {
    UIView *item = [[UIView alloc] init];
    
    UILabel *envLabel = [self envLabelWithFrame:CGRectMake(16, 14, 60, 16)
                                           font:[UIFont systemFontOfSize:16]
                                          color:KNNENv_rgba(97,97,97,1)
                                           text:env
                                  textAlignment:NSTextAlignmentLeft];
    [item addSubview:envLabel];
    
    UILabel *hostLabel = [self envLabelWithFrame:CGRectMake(CGRectGetMaxX(envLabel.frame) + 20, 14, KNNENvWidth - 36 - 76, 16)
                                            font:[UIFont systemFontOfSize:16]
                                           color:KNNENv_rgba(97,97,97,1)
                                            text:dnsHost
                                   textAlignment:NSTextAlignmentLeft];
    [item addSubview:hostLabel];
    
    UIButton *copyBtn = [[UIButton alloc] initWithFrame:CGRectMake(KNNENvWidth - 36 - 16, 14, 36, 16)];
    [copyBtn setTitle:@"复制" forState:UIControlStateNormal];
    copyBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    [copyBtn setTitleColor:KNNENv_rgba(168,173,178,1)  forState:UIControlStateNormal];
    copyBtn.layer.borderColor = KNNENv_rgba(168,173,178,1).CGColor;
    copyBtn.layer.borderWidth = 1;
    copyBtn.tag = self.copyBlocks.count;
    void (^copyAction) (void) = ^() {
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = hostLabel.text;
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"复制成功" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }]];
        [self presentViewController:alert animated:YES completion:nil];
    };
    
    [self.copyBlocks addObject:copyAction];
    
    [copyBtn addTarget:self action:@selector(btnDid:) forControlEvents:UIControlEventTouchUpInside];
    [item addSubview:copyBtn];
    
    UIView *line = [self lineWithFrame:CGRectMake(16,43, KNNENvWidth - 16 * 2, 1)];
    [item addSubview:line];
    
    return item;
}

- (UIView *)methodViewWithFrame:(CGRect)frame
                          title:(NSString *)title
                     rightTitle:(NSString *)rightTitle
                         action:(void (^)(void))actionBlcok{
    UIView *methodView = [[UIView alloc] initWithFrame:frame];
    
    UIButton *clearBtn = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width - 69 - 16 + 10, (frame.size.height - 26)/2 , 69, 26)];
    [clearBtn setTitle:rightTitle forState:UIControlStateNormal];
    [clearBtn setTitleColor:KNNENv_rgba(41,107,255,1) forState:UIControlStateNormal];
    clearBtn.layer.borderColor = KNNENv_rgba(41,107,255,1).CGColor;
    clearBtn.layer.borderWidth = 1;
    clearBtn.layer.cornerRadius = 8;
    clearBtn.clipsToBounds = YES;
    clearBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [clearBtn addTarget:self action:@selector(relationBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    clearBtn.tag = self.relationBlocks.count;
    [self.relationBlocks addObject:actionBlcok];
    [methodView addSubview:clearBtn];
    
    UITextField *input = [[UITextField alloc] initWithFrame:CGRectMake(16,
                                                                       (frame.size.height - 27) / 2,
                                                                       frame.size.width - 16 * 2 - 10 - CGRectGetWidth(clearBtn.frame), 27)];
    input.placeholder = @"点击右边扫码快速获取";
    input.clearButtonMode= UITextFieldViewModeAlways;
    input.layer.borderColor = KNNENv_rgba(200,199,204,1).CGColor;
    input.layer.borderWidth = 1;
    input.layer.cornerRadius = 8;
    input.clipsToBounds = YES;
    input.font = [UIFont systemFontOfSize:12];
    input.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 0)];
    input.leftViewMode = UITextFieldViewModeAlways;
    [methodView addSubview:input];
    self.inputF = input;
    
    return methodView;
}

- (UILabel*)envLabelWithFrame:(CGRect)aframe
                         font:(UIFont*)afont
                        color:(UIColor*)acolor
                         text:(NSString*)atext
                textAlignment:(NSTextAlignment)aalignment {
    UILabel *baseLabel=[[UILabel alloc] initWithFrame:aframe];
    if(afont)baseLabel.font=afont;
    if(acolor)baseLabel.textColor=acolor;
    baseLabel.text=atext;
    baseLabel.textAlignment=aalignment;
    baseLabel.baselineAdjustment=UIBaselineAdjustmentAlignCenters;
    if(aframe.size.height>20){
        baseLabel.numberOfLines=0;
    }
    if (!aframe.size.height) {
        baseLabel.numberOfLines=0;
        CGSize size = [baseLabel sizeThatFits:CGSizeMake(aframe.size.width, MAXFLOAT)];
        aframe.size.height = size.height;
        baseLabel.frame = aframe;
    }else if (!aframe.size.width) {
        CGSize size = [baseLabel sizeThatFits:CGSizeMake(MAXFLOAT, 20)];
        aframe.size.width = size.width;
        baseLabel.frame = aframe;
    }
    //    baseLabel.adjustsFontSizeToFitWidth=YES
    baseLabel.backgroundColor=[UIColor clearColor];
    baseLabel.highlightedTextColor=acolor;//[UIColor whiteColor];
    return baseLabel;// autorelease];
}

- (NSDictionary *)dataDic {
    if (_dataDic == nil) {
        NSDictionary *dataForBeta = @{
                                     @"title":@"BETA",
                                     @"titleDesc":@"",
                                     @"envDesc" : @"BETA为测试环境，进行开发(修改)时，一般新开发（修改）的功能",
                                     @"netDesc":@"需连接公司内网",
                                     @"ENVList":@[@[@"BETA1",@"http://192.168.111.81:9900"]
                                                  ]
                                     };
        
        NSDictionary *dataForProd = @{
                                      @"title":@"PROD",
                                      @"titleDesc":@"",
                                      @"envDesc" : @"PROD为正式环境，即线上环境，生产环境。",
                                      @"netDesc":@"联网即可",
                                      @"ENVList":@[@[@"PROD",@"https://chaojihua.geexwin.com"]]
                                      };
        
        NSDictionary *dataForMock = @{
                                      @"title":@"MOCK",
                                      @"titleDesc":@"STB/dsajdhaks",
                                      @"envDesc" : @"MOCK为模拟环境，通过【移动端>模拟接口工具】可以对请求接口进行快捷的修改操作。这样，只要接口定义好，即使后台环境不稳定，也不再影响前端的基本联调测试。",
                                      @"netDesc":@"公司内网",
                                      @"ENVList":@[@[@"MOCK",@"http://192.168.12.25:7778"]]
                                      };
        
        _dataDic = @{@"BETA":dataForBeta,
                     @"PROD":dataForProd,
                     @"MOCK":dataForMock
                     };
    }
    return _dataDic;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
}

- (NSMutableArray *)copyBlocks {
    if (_copyBlocks == nil) {
        _copyBlocks = [NSMutableArray array];
    }
    return _copyBlocks;
}

- (NSMutableArray *)relationBlocks {
    if (_relationBlocks == nil) {
        _relationBlocks = [NSMutableArray array];
    }
    return _relationBlocks;
}

//- (MZGETMockAPi *)mockApi {
//    if (_mockApi == nil) {
//        _mockApi = [[MZGETMockAPi alloc] init];
//    }
//    return _mockApi;
//}

- (void)handleKeyShow:(NSNotification *)notification {
    
    CGFloat time = [notification.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    
    CGFloat keyBordH = [notification.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue].size.height;
    
    CGFloat maxY = CGRectGetMaxY(self.inputRelativeFrame);
    
    CGFloat moveValue =  maxY + keyBordH - KNNENvHeight;
    
    if (moveValue < 0) {
        moveValue = 0;
    }
    
    [UIView animateWithDuration:time
     
                     animations:^{
                         self.bgScrollView.contentOffset = CGPointMake(0, moveValue);
                     }];
    
}

- (void)handleHiden:(NSNotification *)notification {
    
    CGFloat time = [notification.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    
    [UIView animateWithDuration:time
     
                     animations:^{
                         self.bgScrollView.contentOffset = CGPointMake(0, 0);
                     }
                     completion:^(BOOL finished) {
                     }];
}

- (UIView *)startloadView {
    UIWindow *keyWindow =  [[UIApplication sharedApplication].windows firstObject];
    
    UIView *loadView = [[UIView alloc] initWithFrame:keyWindow.frame];
    loadView.backgroundColor = KNNENv_rgba(0, 0, 0, 0.5);
    [keyWindow addSubview:loadView];
    
    UILabel *label = [self envLabelWithFrame:CGRectMake(0, (loadView.frame.size.height - 60) / 2, loadView.frame.size.width, 60)
                                        font:[UIFont systemFontOfSize:20]
                                       color:[UIColor whiteColor]
                                        text:@"正在开启..."
                               textAlignment:NSTextAlignmentCenter];
    [loadView addSubview:label];
    return loadView;
}

- (UIBarButtonItem *)rightBarButton{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBounds:CGRectMake(0, 0, 44, 44)];
    button.backgroundColor = [UIColor clearColor];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button setTitle:@"退下"
            forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(dissBtn) forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)dissBtn {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

